home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP11.ZIP / CHAP11 / COSCHMOO / COSCHMOO.CPP < prev    next >
C/C++ Source or Header  |  1993-06-15  |  14KB  |  615 lines

  1. /*
  2.  * COSCHMOO.CPP
  3.  * Component Schmoo Chapter 11
  4.  *
  5.  * WinMain and CSchmooFrame implementations.
  6.  *
  7.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Software Design Engineer
  10.  * Microsoft Systems Developer Relations
  11.  *
  12.  * Internet  :  kraigb@microsoft.com
  13.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  14.  */
  15.  
  16.  
  17.  
  18. #define INITGUIDS
  19. #include "coschmoo.h"
  20.  
  21.  
  22. /*
  23.  * WinMain
  24.  *
  25.  * Purpose:
  26.  *  Main entry point of application.   Should register the app class
  27.  *  if a previous instance has not done so and do any other one-time
  28.  *  initializations.
  29.  */
  30.  
  31. int PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR pszCmdLine, int nCmdShow)
  32.     {
  33.     LPCSchmooFrame  pFR;
  34.     FRAMEINIT       fi;
  35.     WPARAM          wRet;
  36.  
  37.    #ifndef WIN32
  38.     SetMessageQueue(96);
  39.    #endif
  40.  
  41.     //Attempt to allocate and initialize the application
  42.     pFR=new CSchmooFrame(hInst, hPrev, pszCmdLine, nCmdShow);
  43.  
  44.     fi.idsMin=IDS_FRAMEMIN;
  45.     fi.idsMax=IDS_FRAMEMAX;
  46.     fi.idsStatMin=IDS_STATMESSAGEMIN;
  47.     fi.idsStatMax=IDS_STATMESSAGEMAX;
  48.     fi.idStatMenuMin=ID_MENUFILE;
  49.     fi.idStatMenuMax=ID_MENUHELP;
  50.     fi.iPosWindowMenu=WINDOW_MENU;
  51.     fi.cMenus=CMENUS;
  52.  
  53.     //If we can initialize pFR, start chugging messages
  54.     if (pFR->FInit(&fi))
  55.         wRet=pFR->MessageLoop();
  56.  
  57.     delete pFR;
  58.     return wRet;
  59.     }
  60.  
  61.  
  62.  
  63.  
  64. /*
  65.  * CSchmooFrame::CSchmooFrame
  66.  * CSchmooFrame::~CSchmooFrame
  67.  *
  68.  * Constructor Parameters:
  69.  *  hInst           HINSTANCE from WinMain
  70.  *  hInstPrev       HINSTANCE from WinMain
  71.  *  pszCmdLine      LPSTR from WinMain
  72.  *  nCmdShow        int from WInMain
  73.  */
  74.  
  75. CSchmooFrame::CSchmooFrame(HINSTANCE hInst, HINSTANCE hInstPrev
  76.     , LPSTR pszCmdLine, int nCmdShow)
  77.     : CFrame(hInst, hInstPrev, pszCmdLine, nCmdShow)
  78.     {
  79.     UINT        i;
  80.  
  81.     for (i=0; i<5; i++)
  82.         m_hBmpLines[i]=NULL;
  83.  
  84.     m_fInitialized=FALSE;
  85.     return;
  86.     }
  87.  
  88.  
  89. CSchmooFrame::~CSchmooFrame(void)
  90.     {
  91.     UINT            i;
  92.  
  93.     for (i=0; i<5; i++)
  94.         {
  95.         if (NULL!=m_hBmpLines[i])
  96.             DeleteObject(m_hBmpLines[i]);
  97.         }
  98.  
  99.     //CHAPTER8MOD
  100.     OleFlushClipboard();
  101.     //End CHAPTER8MOD
  102.  
  103.     if (m_fInitialized)
  104.         CoUninitialize();
  105.  
  106.     return;
  107.     }
  108.  
  109.  
  110.  
  111.  
  112.  
  113. /*
  114.  * CSchmooFrame::FInit
  115.  *
  116.  * Purpose:
  117.  *  Call CoInitialize then calling down into the base class
  118.  *  initialization.
  119.  *
  120.  * Parameters:
  121.  *  pFI             LPFRAMEINIT containing initialization parameters.
  122.  *
  123.  * Return Value:
  124.  *  BOOL            TRUE if initialization succeeded, FALSE otherwise.
  125.  */
  126.  
  127. BOOL CSchmooFrame::FInit(LPFRAMEINIT pFI)
  128.     {
  129.     DWORD       dwVer;
  130.  
  131.     dwVer=OleBuildVersion();
  132.  
  133.     if (rmm!=HIWORD(dwVer))
  134.         return FALSE;
  135.  
  136.     if (FAILED(OleInitialize(NULL)))
  137.         return FALSE;
  138.  
  139.     m_fInitialized=TRUE;
  140.  
  141.     return CFrame::FInit(pFI);
  142.     }
  143.  
  144.  
  145.  
  146.  
  147.  
  148. /*
  149.  * CSchmooFrame::CreateCClient
  150.  *
  151.  * Purpose:
  152.  *  Constructs a new client specific to the application.
  153.  *
  154.  * Parameters:
  155.  *  None
  156.  *
  157.  * Return Value:
  158.  *  LPCClient       Pointer to the new client object.
  159.  */
  160.  
  161. LPCClient CSchmooFrame::CreateCClient(void)
  162.     {
  163.     return (LPCClient)(new CSchmooClient(m_hInst));
  164.     }
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171. /*
  172.  * CSchmooFrame::FPreShowInit
  173.  *
  174.  * Purpose:
  175.  *  Called from FInit before intially showing the window.  We do whatever
  176.  *  else we want here, modifying nCmdShow as necessary which affects
  177.  *  ShowWindow in FInit.
  178.  *
  179.  * Parameters:
  180.  *  None
  181.  *
  182.  * Return Value:
  183.  *  BOOL            TRUE if this initialization succeeded, FALSE otherwise.
  184.  */
  185.  
  186. BOOL CSchmooFrame::FPreShowInit(void)
  187.     {
  188.     CreateLineMenu();
  189.     CheckLineSelection(IDM_LINESOLID);
  190.     m_pGB->Check(IDM_LINESOLID, TRUE);
  191.     return TRUE;
  192.     }
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199. /*
  200.  * CSchmooFrame::CreateLineMenu
  201.  *
  202.  * Purpose:
  203.  *  Initializes the bitmaps used to create the Line menu and replaces
  204.  *  the text items defined in the application resources with these
  205.  *  bitmaps.  Note that the contents of m_hBmpLines must be cleaned
  206.  *  up when the application terminates.
  207.  *
  208.  * Parameters:
  209.  *  None
  210.  *
  211.  * Return Value:
  212.  *  None
  213.  */
  214.  
  215. void CSchmooFrame::CreateLineMenu(void)
  216.     {
  217.     HMENU       hMenu;
  218.     HDC         hDC, hMemDC;
  219.     HPEN        hPen;
  220.     HGDIOBJ     hObj;
  221.     TEXTMETRIC  tm;
  222.     UINT        i, cx, cy;
  223.  
  224.  
  225.     hMenu=GetSubMenu(GetMenu(m_hWnd), 3);   //Line menu.
  226.     hDC=GetDC(m_hWnd);
  227.  
  228.     //Create each line in a menu item 8 chars wide, one char high.
  229.     GetTextMetrics(hDC, &tm);
  230.     cx=tm.tmAveCharWidth*8;
  231.     cy=tm.tmHeight;
  232.  
  233.     //Create a memory DC in which to draw lines, and bitmaps for each line.
  234.     hMemDC=CreateCompatibleDC(hDC);
  235.     ReleaseDC(m_hWnd, hDC);
  236.  
  237.     for (i=0; i<5; i++)
  238.         {
  239.         m_hBmpLines[i]=CreateCompatibleBitmap(hMemDC, cx, cy);
  240.         SelectObject(hMemDC, m_hBmpLines[i]);
  241.  
  242.         PatBlt(hMemDC, 0, 0, cx, cy, WHITENESS);
  243.  
  244.         hPen=CreatePen(i, 1, 0L);           //i==line style like PS_SOLID
  245.         hObj=SelectObject(hMemDC, hPen);
  246.  
  247.         MoveTo(hMemDC, 0,  cy/2);
  248.         LineTo(hMemDC, cx, cy/2);
  249.  
  250.         ModifyMenu(hMenu, IDM_LINEMIN+i, MF_BYCOMMAND | MF_BITMAP
  251.             , IDM_LINEMIN+i, (LPCSTR)MAKELONG(m_hBmpLines[i], 0));
  252.  
  253.         SelectObject(hMemDC, hObj);
  254.         DeleteObject(hPen);
  255.         }
  256.  
  257.     CheckMenuItem(hMenu, IDM_LINESOLID, MF_CHECKED);
  258.     DeleteDC(hMemDC);
  259.  
  260.     return;
  261.     }
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271. /*
  272.  * CSchmooFrame::CreateGizmos
  273.  *
  274.  * Purpose:
  275.  *  Procedure to create all the necessary gizmobar buttons.
  276.  *
  277.  * Parameters:
  278.  *  None
  279.  *
  280.  * Return Value:
  281.  *  UINT            Number of gizmos added to the bar.
  282.  */
  283.  
  284. UINT CSchmooFrame::CreateGizmos(void)
  285.     {
  286.     UINT            iLast;
  287.     UINT            uState=GIZMO_NORMAL;
  288.     UINT            utCmd =GIZMOTYPE_BUTTONCOMMAND;
  289.     UINT            utEx  =GIZMOTYPE_BUTTONATTRIBUTEEX;
  290.  
  291.     //Insert the standard ones.
  292.     iLast=CFrame::CreateGizmos();
  293.  
  294.     //Insert File Import in the 5th position and account for it in iLast.
  295.     m_pGB->Add(utCmd, 4, IDM_FILEIMPORT, m_dxB, m_dyB, NULL, m_hBmp, 2, uState);
  296.     iLast++;
  297.  
  298.     //Separator
  299.     m_pGB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB, NULL, NULL, 0, uState);
  300.  
  301.     //For the Background bitmap, preserve our use of black (part of the image)
  302.     m_pGB->Add(utCmd, iLast++, IDM_COLORBACKGROUND, m_dxB, m_dyB, NULL, m_hBmp, 3
  303.                , GIZMO_NORMAL | PRESERVE_BLACK);
  304.  
  305.     m_pGB->Add(utCmd, iLast++, IDM_COLORLINE, m_dxB, m_dyB, NULL, m_hBmp, 4, uState);
  306.  
  307.     //Separator
  308.     m_pGB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB, NULL, NULL, 0, uState);
  309.  
  310.     //Line styles.
  311.     m_pGB->Add(utEx, iLast++, IDM_LINESOLID,      m_dxB, m_dyB, NULL, m_hBmp, 5, uState);
  312.     m_pGB->Add(utEx, iLast++, IDM_LINEDASH,       m_dxB, m_dyB, NULL, m_hBmp, 6, uState);
  313.     m_pGB->Add(utEx, iLast++, IDM_LINEDOT,        m_dxB, m_dyB, NULL, m_hBmp, 7, uState);
  314.     m_pGB->Add(utEx, iLast++, IDM_LINEDASHDOT,    m_dxB, m_dyB, NULL, m_hBmp, 8, uState);
  315.     m_pGB->Add(utEx, iLast++, IDM_LINEDASHDOTDOT, m_dxB, m_dyB, NULL, m_hBmp, 9, uState);
  316.  
  317.     return iLast;
  318.     }
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327. /*
  328.  * CSchmooFrame::OnCommand
  329.  *
  330.  * Purpose:
  331.  *  WM_COMMAND handler for the Schmoo frame window that just processes
  332.  *  the line menu and the color menu leaving the CFrame to do everything
  333.  *  else.
  334.  *
  335.  * Parameters:
  336.  *  hWnd            HWND of the frame window.
  337.  *  wParam          WPARAM of the message.
  338.  *  lParam          LPARAM of the message.
  339.  *
  340.  * Return Value:
  341.  *  LRESULT         Return value for the message.
  342.  */
  343.  
  344. LRESULT CSchmooFrame::OnCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
  345.     {
  346.     LPCSchmooDoc    pDoc;
  347.     char            szFile[CCHPATHMAX];
  348.     BOOL            fOK;
  349.     UINT            i, uTemp;
  350.     COLORREF        rgColors[16];
  351.     CHOOSECOLOR     cc;
  352.  
  353.     COMMANDPARAMS(wID, wCode, hWndMsg);
  354.  
  355.     /*
  356.      * Don't bother with anything during first initialization,
  357.      * skipping many GizmoBar notifications.
  358.      */
  359.     if (m_fInit)
  360.         return 0L;
  361.  
  362.     pDoc=(LPCSchmooDoc)m_pCL->Ac